﻿/*
 * Сделано в SharpDevelop.
 * Пользователь: Badanin M.V.
 * Дата: 06.08.2013
 * Время: 13:46
 * Проект: РПН Регион 2013
 * Распространяется под лицензией GPL v3
 */
using System;
using System.Text.RegularExpressions;
using System.Linq;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Data.Sql;
using System.Collections.Generic;

namespace Region
{
  [Database]
  public class BD_IP: DataContext
  {
  	#if DEBUG
    //public BD_IP() : base(@"Data Source=BADANIN21\SQLEXPRESS;Initial Catalog=BD_IP;Integrated Security=True;Pooling=False ") { }  	
    public BD_IP(Engine engine):base(engine.config.getValue<String>("BD_IP","")){}
    #else
    public BD_IP(Engine engine):base(engine.config.getValue<String>("BD_IP","")){}
    #endif
    public Table<IP> IP;//{get{return this.GetTable<>}}
    public void copyRegionToIP(Engine engine)
    {
        try
        {
            var list = new KontList(engine);
            list.setWhere("OGRN_LENGTH", 15);
            list.Filter();

            int NumLines = list.NumLines;
            for (int i = 0; i < list.NumLines; i++)
            {
                var kont = new Kont(engine, list.getLine(i).KontID);
                var ip_list1 = from t in this.IP select t;
                var ip_list = from t in this.IP where t.INN == kont.INN && t.OGRN == kont.OGRN select t;
                IP ip;
                if (ip_list.Count() > 0)
                    ip = ip_list.First();
                else
                {
                    ip = new Region.IP();
                    ip.INN = kont.INN;
                    ip.OGRN = kont.OGRN;
                    IP.InsertOnSubmit(ip);
                }

                if (ip.FIO==null || ip.FIO.Length == 0 || kont.KontName.Length < ip.FIO.Length) 
                    ip.FIO = kont.KontName.Replace("индивидуальный", "").Replace("Индивидуальный", "").Replace("предприниматель", "").Trim();
                ip.Telefon = kont.KontTelefon;
                ip.Post = kont.KontPostIndex;
                ip.Region = kont.KontRegion;
                ip.Raion = kont.KontRaion;
                ip.Gorod = kont.KontGorod;
                ip.Naspunkt = kont.KontNaspunkt;
                ip.Street = kont.KontStreet;
                ip.Dom = kont.KontDom;
                ip.Korpus = kont.KontKorpus;
                ip.Kvart = kont.KontKvart;
                this.SubmitChanges();
            }
            
        }
        catch (Exception)
        {
        }
    }
  }
  /// <summary>
  /// Description of IP.
  /// </summary>
  [Table] public class IP
  {
    [Column( IsPrimaryKey = true)] public String OGRN{get;set;}
    [Column( IsPrimaryKey = true)] public String INN{get;set;}
    [Column] public String FIO{get;set;}
    [Column] public String Telefon{get;set;}    
    [Column] public String Post{get;set;}
    [Column] public String Region{get;set;}
    [Column] public String Raion{get;set;}
    [Column] public String Gorod{get;set;}
    [Column] public String Naspunkt{get;set;}
    [Column] public String Street{get;set;}
    [Column] public String Dom{get;set;}
    [Column] public String Korpus{get;set;}
    [Column] public String Kvart{get;set;}
    
  }
  public class IPList
  {
      public struct Line
      {
        public IP ID; 
        public String INN;
        public String OGRN;
        public String FIO;
      }
      
      private Engine m_engine;
      private List<Line> m_lines;
      private Dictionary<String, Object> m_wheres;
      public Line getLine(Int32 l) { return this.m_lines[l]; }
      public Int32 NumLines { get { return m_lines.Count; } }
      private Int32 m_FullLines;
      public Int32 FullLines { get { return m_FullLines; } }


      public IPList(Engine engine)
      {
          m_engine = engine;
          m_lines = new List<Line>();
          m_wheres = new Dictionary<String, Object>();
      }
      public bool clearWhere()
      {
          m_wheres = new Dictionary<String, Object>();
          return true;
      }
      public bool setWhere(String Where, Object Value)
      {
          m_wheres[Where] = Value;
          return true;
      }
      public bool Filter()
      {
          var Query = from ip in m_engine.bd_IP.IP select ip;
          
          if (m_wheres.ContainsKey("INN") && m_wheres["INN"] != null && ((String)m_wheres["INN"]).Length>1)
            Query = Query.Where(p => p.INN.Contains((String)m_wheres["INN"]));
          
          foreach (var q in Query)
          {
              Line l = new Line();
              l.ID = q;
              l.INN = q.INN;
              l.OGRN = q.OGRN;
              l.FIO = q.FIO;
              m_lines.Add(l);
          }
          return true;
      }
  }

}
